package com.zy.edu.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zy.edu.bean.CourseChapterVideoCls;
import com.zy.edu.bean.CourseChapterVideoCls;
import com.zy.edu.bean.InputParam;
import com.zy.edu.bean.ResponseInfo;
import com.zy.edu.config.SystemParam;
import com.zy.edu.dao.CourseChapterVideoClsDao;
import com.zy.edu.service.CourseChapterVideoClsService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HuangHaiShui
 * @since 2021-07-29
 */
@Service
public class CourseChapterVideoClsServiceImpl extends ServiceImpl<CourseChapterVideoClsDao, CourseChapterVideoCls> implements CourseChapterVideoClsService {
    @Override
    public Map saveOrUpdate(CourseChapterVideoCls courseChapterVideoCls, InputParam inputParam) {
        if(StringUtils.isEmpty(courseChapterVideoCls.getVideoId())
                ||StringUtils.isEmpty(courseChapterVideoCls.getClassId())){
            return ResponseInfo.error(SystemParam.ERROR_PARAMS);
        }

        EntityWrapper<CourseChapterVideoCls> temp = new EntityWrapper<>();
        temp.eq("video_id",courseChapterVideoCls.getVideoId())
                .eq("class_id",courseChapterVideoCls.getClassId());

        List<CourseChapterVideoCls> list = this.baseMapper.selectList(temp);

        if (list.size()==0) {
            courseChapterVideoCls.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            courseChapterVideoCls.setCreateTime(new Date());
            courseChapterVideoCls.setFinished(0);
            this.baseMapper.insert(courseChapterVideoCls);
        } else {
            courseChapterVideoCls.setId(list.get(0).getId());
            //需要记录上次观看的时间点
            this.baseMapper.updateById(courseChapterVideoCls);
        }
        return ResponseInfo.success(null);
    }

    @Override
    public Map dele(CourseChapterVideoCls courseChapterVideoCls) {
        if(StringUtils.isEmpty(courseChapterVideoCls.getVideoId())
                ||StringUtils.isEmpty(courseChapterVideoCls.getClassId())){
            return ResponseInfo.error(SystemParam.ERROR_PARAMS);
        }
        this.baseMapper.delete(new EntityWrapper<CourseChapterVideoCls>()
                .eq("video_id",courseChapterVideoCls.getVideoId())
                .eq("class_id",courseChapterVideoCls.getClassId()));
        return ResponseInfo.success(null);
    }

    @Override
    public Map info(CourseChapterVideoCls courseChapterVideoCls, InputParam inputParam) {

        if(StringUtils.isEmpty(courseChapterVideoCls.getVideoId())
                ||StringUtils.isEmpty(courseChapterVideoCls.getClassId())){
            return ResponseInfo.error(SystemParam.ERROR_PARAMS);
        }

        EntityWrapper<CourseChapterVideoCls> temp = new EntityWrapper<>();
        temp.eq("video_id",courseChapterVideoCls.getVideoId())
                .eq("class_id",courseChapterVideoCls.getClassId());

        List<CourseChapterVideoCls> list = this.baseMapper.selectList(temp);
        if (list.size()>0) {
            return ResponseInfo.success(list.get(0));
        }
        return ResponseInfo.success(null);
    }
}
